<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="订单号">
                <a-input placeholder="请输入订单号" v-model="queryParam.order_no" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="门店名称">
                <a-input placeholder="请输入门店名称" v-model="queryParam.md_name" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="起止时间">
                <div class="select">
                  <a-range-picker
                    format="YYYY-MM-DD HH:mm"
                    valueFormat="YYYY-MM-DD HH:mm"
                    :placeholder="['开始时间', '结束时间']"
                    v-model="queryTime"
                    @change="changeTime"
                  />
                  <a-button type="primary" @click="getProfitist" style="margin-left: 10px">查询</a-button>
                  <a-button :loading="exporting" @click="exportP" type="primary" style="margin-left: 9px">导出Excel</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.is_send" @change="getProfitist">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="1"> 已到账 </a-radio-button>
          <a-radio-button value="0"> 未到账 </a-radio-button>
        </a-radio-group>
        <!-- <a-button>导出报表</a-button> -->
      </div>
      <!-- <a-alert type="info" show-icon style="margin-bottom: 10px">
        <span slot="message">
          已选择 <span style="color: red">{{ selectedRowKeys.length }}</span> 项
        </span>
      </a-alert> -->
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="shopList"
        :scroll="{ x: true }"
      >
        <div slot="name" slot-scope="text, record">
          <div class="name">
            <div class="pic">
              <img :src="record.cover_url" alt="" />
            </div>
            <div class="info">
              <span>{{ record.md_name }}</span>
            </div>
          </div>
        </div>
        <div slot="status" slot-scope="scope">
          <div class="status">
            <span :class="scope.is_send == 0 ? 'grey' : 'green'"></span>
            <span>{{ scope.is_send == 0 ? '未到账' : '已到账' }}</span>
          </div>
        </div>
        <span slot="updated_at" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <span slot="created_at" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <div slot="action" slot-scope="text, scope">
          <div class="action" v-if="scope.shop_audit == 0">
            <a-button type="link" @click="audit(scope.id, 1)" size="small">通过</a-button>
            <span class="line"></span>
            <a-button type="link" @click="audit(scope.id, 2)" size="small">拒绝</a-button>
          </div>
        </div>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { profit, expProfit } from '@/api/outlet'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '门店名称',
    scopedSlots: {
      customRender: 'name',
    },
    width: 200,
  },
  {
    title: '昵称',
    dataIndex: 'user_name',
    width: 100,
  },
  {
    title: '下单时间',
    dataIndex: 'created_at',
    width: 200,
    scopedSlots: {
      customRender: 'created_at',
    },
    sorter: (a, b) => a.created_at - b.created_at,
  },
  {
    title: '订单号',
    dataIndex: 'order_no',
    width: 120,
  },
  {
    title: '销售佣金',
    dataIndex: 'pay_profit',
    sorter: (a, b) => a.sale_profit - b.sale_profit,
    width: 110,
  },
  {
    title: '核销佣金',
    dataIndex: 'clerk_profit',
    sorter: (a, b) => a.clerk_profit - b.clerk_profit,
    width: 110,
  },
  {
    title: '利润佣金',
    dataIndex: 'sale_profit',
    sorter: (a, b) => a.pay_profit - b.pay_profit,
    width: 110,
  },
  {
    title: '总佣金',
    dataIndex: 'total_profit',
    sorter: (a, b) => a.total_profit - b.total_profit,
    width: 110,
  },
  {
    title: '操作时间',
    dataIndex: 'updated_at',
    width: 200,
    scopedSlots: {
      customRender: 'updated_at',
    },
    sorter: (a, b) => a.updated_at - b.updated_at,
  },
  {
    title: '佣金状态',
    width: 100,
    scopedSlots: {
      customRender: 'status',
    },
  },
]

export default {
  data() {
    return {
      exporting: false,
      // 查询参数
      queryParam: {
        order_no: null,
        md_name: null,
        is_send: '-1',
        start_time: null,
        end_time: null,
        pageNo: 1,
        pageSize: 10,
      },
      // 门店列表
      shopList: [],
      columns,
      // 数据总条数
      totalCount: 0,
      // 起止时间数组
      queryTime: [],
    }
  },
  methods: {
    exportP(e) {
      this.exporting = true;
      let parameter = {...this.queryParam};
      Object.assign(parameter, {
        pageNo: 1,
        pageSize: 100000,
        _filename: '门店佣金.xlsx',
        export: 1
      })
      expProfit(parameter).then(()=>{
        this.exporting = false;
      });
    },
    // 获取门店列表
    async getProfitist() {
      const hide = this.$message.loading('加载中', 0)
      const res = await profit(this.queryParam)
      console.log(res)
      if (res.code == 0) {
        this.totalCount = res.data.totalCount
        this.shopList = res.data.data
      }
      hide()
    },
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getProfitist()
    },
    // 切换筛选时间
    changeTime(e) {
      if (e.length) {
        this.queryParam.start_time = e[0]
        this.queryParam.end_time = e[1]
      } else {
        this.queryParam.start_time = null
        this.queryParam.end_time = null
      }
    },
  },
  created() {
    this.getProfitist()
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-top: 14px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-top: 14px;
    margin-right: 20px;
  }
  .ant-btn {
    margin-top: 14px;
  }
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 4px;
  }
  .green {
    background-color: #65c817;
  }
  .grey {
    background-color: #c6c6c6;
  }
}
.action {
  display: flex;
  align-items: center;
  .line {
    width: 1px;
    height: 14px;
    background-color: #d8d8d8;
  }
}
</style>